*Este es el dofile central del proyecto. Aquí se construyen los diferentes indicadores de DWMS, los impactos sobre DWMS y PLANEA, además de estadísticas descriptivas.

*****@@@@@@@@@@@@@@@@@@@*****
*****************************
*****Procesamiento DWMS******
*****************************
*****@@@@@@@@@@@@@@@@@@@*****


clear
set more off


use "$basein/DMWS Corregido/Base_Reclasficiacion_Final_25012019"

***Elimino escuelas que pertenecen al piloto***

drop if fase==0

*********************************************
**Convierto variables en letras a numericas**
*********************************************

drop *_obs* punto

foreach var of varlist p* {

	replace `var'="1" if `var'=="a"
	replace `var'="1.5" if `var'=="b"
	replace `var'="2" if `var'=="c"
	replace `var'="2.5" if `var'=="d"
	replace `var'="3" if `var'=="e"
	replace `var'="4" if `var'=="f"
	replace `var'="5" if `var'=="g"
	destring `var', replace force
}

***************************************
**Conteo de preguntas con categoria H** /H hace referencia a Missings. Es decir, la información otorgada por la entrevista no permite otorgar una calificación.
***************************************

egen Missings=rowmiss(p*)

sum Missings, detail

hist Missings, freq xtitle(Missing questions) ytitle(Frecuencia) graphregion(color(white))

*******************************
***Construyo los indicadores***
*******************************

rename p1_1 leader2_1
rename p1_2 leader2_2
rename p1_3 leader2_3

rename p2_1 lean7_1
rename p2_2 lean7_2
rename p2_3 lean7_3

rename p3_1 lean9_1 
rename p3_2 lean9_2
rename p3_3 lean9_3

rename p4_1 lean8_1
rename p4_2 lean8_2
rename p4_3 lean8_3

rename p5_1 lean10_1
rename p5_2 lean10_2
rename p5_3 lean10_3

rename p6_1 perf1_1
rename p6_2 perf1_2
rename p6_3 perf1_3

rename p7_1 perf2_1
rename p7_2 perf2_2
rename p7_3 perf2_3

rename p8_1 perf3_1
rename p8_2 perf3_2
rename p8_3 perf3_3

rename p9_1 perf4_1
rename p9_2 perf4_2
rename p9_3 perf4_3

rename p10_1 perf5_1
rename p10_2 perf5_2
rename p10_3 perf5_3

rename p11_1 perf6_1
rename p11_2 perf6_2
rename p11_3 perf6_3

rename p12_1 perf7_1
rename p12_2 perf7_2
rename p12_3 perf7_3

rename p13_1 perf8_1
rename p13_2 perf8_2
rename p13_3 perf8_3

rename p14_1 perf9_1
rename p14_2 perf9_2
rename p14_3 perf9_3

rename p15_1 leader3_1
rename p15_2 leader3_2

rename p16_1 leader4_1
rename p16_2 leader4_2
rename p16_3 leader4_3

rename p17_1 perf10_1
rename p17_2 perf10_2
rename p17_3 perf10_3

rename p18_1 talent2_1
rename p18_2 talent2_2
rename p18_3 talent2_3

rename p19_1 talent3_1
rename p19_2 talent3_2
rename p19_3 talent3_3

rename p20_1 talent4_1
rename p20_2 talent4_2

rename p22_1 talent6_1
rename p22_2 talent6_2
rename p22_3 talent6_3

rename p23_1 talent5_1
rename p23_2 talent5_2
rename p23_3 talent5_3

******************************
***Se agregan por temáticas***
******************************



foreach var in leader2 leader4 lean7 lean8 lean9 lean10 perf1 perf2 perf3 perf4 perf5 perf6 perf7 perf8 perf9 perf10 talent2 talent3  talent5 talent6 {
	egen `var'=rmean(`var'_1 `var'_2 `var'_3)
	lab var `var' "MGMT: mean of `var' processes"
	pca `var'_1 `var'_2 `var'_3
	predict `var'_pca, score
}


foreach var in leader3 talent4  {
	egen `var'=rmean(`var'_1 `var'_2 )
	pca `var'_1 `var'_2 
	predict `var'_pca, score
	lab var `var' "MGMT: mean of `var' processes"
}

*Creo que solo deberiamos tener los que tengan todo!
*Mauricio hizo este cambio el 11 de Sept de 2019
egen missing=rowmiss(leader2 leader4 lean7 lean8 lean9 lean10 perf1 perf2 perf3 perf4 perf5 perf6 perf7 perf8 perf9 perf10 talent2 talent3  talent5 talent6)
drop if missing!=0

****************************************
***Se definen los indicadores finales***
****************************************

egen leader=rmean(leader2 leader3 leader4)
egen operations=rmean(lean7 lean8 lean9 lean10)
egen monitoring=rmean(perf1 perf2 perf3 perf4 perf5)
egen targets=rmean(perf6 perf7 perf8 perf9 perf10 )
egen people=rmean( talent2 talent3 talent4 talent5 talent6)
egen index=rmean(leader2 leader3 leader4 lean7 lean8 lean9 lean10 perf1 perf2 perf3 perf4 perf5 perf6 perf7 perf8 perf9 perf10 talent2 talent3 talent4 talent5 talent6)
egen management=rmean(lean7 lean8 lean9 lean10 perf1 perf2 perf3 perf4 perf5 perf6 perf7 perf8 perf9 perf10 talent2 talent3 talent4 talent5 talent6)


pca leader2 leader3 leader4
predict leader_pca

pca lean7 lean8 lean9 lean10
predict operations_pca

pca perf1 perf2 perf3 perf4 perf5
predict monitoring_pca

pca perf6 perf7 perf8 perf9 perf10
predict targets_pca

pca talent2 talent3 talent4 talent5 talent6
predict people_pca

pca leader2_1- talent5_3
predict index_pca


pca lean7 lean8 lean9 lean10 perf1 perf2 perf3 perf4 perf5 perf6 perf7 perf8 perf9 perf10 talent2 talent3 talent4 talent5 talent6
predict management_pca

**************************************************
***Dejo base para otros ejercicios relacionados***

keep cct fase index  management treatgroup2 estado leader operations monitoring targets people index_pca management_pca  leader_pca operations_pca monitoring_pca targets_pca people_pca 

rename estado estado1

rename index index_nuevo
rename index_pca index_nuevo_pca


duplicates drop cct fase, force

*****************
***Correciones***
*****************

replace estado1=5 if cct=="27DPR1973U"
replace estado1=7 if cct=="17DPR0024G"
replace estado1=7 if cct=="17DST0040J"
replace treatgroup2="PEC" if cct=="21DPR2003U"
replace treatgroup2="PEC" if cct=="21DPR3026L"
replace treatgroup2="PEC" if cct=="21DPR3443Y"


*************************************
***Genero variables de tratamiento***
*************************************

gen PEC_PLUS=0
replace PEC_PLUS=1 if treatgroup2=="PEC Plus"

gen PEC=0
replace PEC=1 if treatgroup2=="PEC"

********************************************************************
****Convierto en panel wide para region controlando por baseline****
********************************************************************

sort cct fase

duplicates tag cct fase, gen(duplicados)

br if duplicados>0

duplicates drop cct fase, force

reshape wide index_nuevo management leader operations monitoring targets people index_nuevo_pca management_pca leader_pca operations_pca monitoring_pca targets_pca people_pca, i(cct) j(fase)

rename index_nuevo* index*


************************
**Base de enumeradores**
************************

merge 1:1 cct using "$basein/Datos adicionales/Enumeradores.dta", gen(_pp)

keep if _pp==3

*********************
**Base de controles**
*********************

rename  cct school_id

merge 1:1 school_id using "$basein/Datos adicionales/base_Contraste.dta", gen(planea)

keep if planea==3

**********************
***Base experimento***
**********************

merge m:1 school_id using "$basein/Datos adicionales/Experimento_id.dta", gen(experimento)

keep if experimento==3

******************
***Base estrato***
******************

merge m:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estratos)

keep if _estratos==3

******************
***KEEP´ONLY PEC VS PEC PLUS******
******************

drop if state==10
drop if treatgroup2=="Control"

*************************************
***Genero variables estandarizadas***
*************************************

foreach x in index  management leader operations monitoring targets people {
	egen `x'_sted1=std(`x'1)
	egen `x'_sted2=std(`x'2)
	egen `x'_sted3=std(`x'3)
	
	egen `x'_pca_sted1=std(`x'_pca1)
	egen `x'_pca_sted2=std(`x'_pca2)
	egen `x'_pca_sted3=std(`x'_pca3)
}

*********************************
***Variables para heterogéneos***
*********************************

gen Marg=0
replace Marg=1 if marginacion=="Muy alta o Alta"

merge 1:1 school_id using "$basein/Datos adicionales/Change_Director_2015_2018.dta", gen(_cambio)

drop if _cambio==2


merge 1:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)

keep if _secundaria==3

merge 1:1 school_id using "$basein/Datos adicionales/Cambios_director_evaluacion.dta", gen(_director)

keep if _director==3

drop if secondary==1
save "$base_out/DWMS_Regresiones", replace



preserve
rename school_id cct_num
keep management_sted3 operations_sted3 index_sted3 monitoring_sted3 targets_sted3 people_sted3   state estrato enumerador treatgroup PEC PEC_PLUS cct_num
save "$base_out/management_summary.dta", replace
restore	

preserve
rename school_id cct_num
keep management_sted1 index_sted1  operations_sted1 monitoring_sted1 targets_sted1 people_sted1   state estrato enumerador treatgroup PEC PEC_PLUS cct_num
save "$base_out/management_summary_2015.dta", replace
restore	


